package com.example.mybatisplugins.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;


/**
 * @author: 晓龙oba
 * @date: 2021/12/3 15:09
 * @description: Mybaits逆向工程
 * 依赖mybatisPlus框架
 * 文档地址: https://mp.baomidou.com/guide/generator.html
 * @version: 1.0.0
 */
public class MysqlCodeReverseUtil {
    /*逆向文件配置*/
    private static final String OUT_PUT_DIR = "文件路径"; //配置文件生成文件夹路径
    private static final String AUTHOR = "作者";// 配置作者
    private static final Boolean SET_OPEN = false; // 文件生成后是否打开目录
    private static final Boolean SET_SWAGGER = true; // 是否配置swagger注解
    private static final String SERVICE_NAME = "I%sService";//配置Service文件名称格式,添加I表示接口
    private static final String ENTITY_NAME = "%sEntity"; // 配置默认的entity文件名称格式
    private static final Boolean SET_LOMBOOK = true; // 是否配置lombook注解
    private static final String PACKAGE_PREFIX = "info.xiaolong";//包名前缀
    /*数据源配置*/
    private static final String DATASOURCE_URL = "jdbc:mysql://127.0.0.1:3306/common_base";//数据库链接地址
    private static final String DATASOURCE_DRIVER = "com.mysql.cj.jdbc.Driver";//数据库驱动
    private static final String DATASOURCE_USERNAME = "root"; //用户名
    private static final String DATASOURCE_PASSWORD = "root"; // 数据库密码
    /*生成策略*/
    private static final Boolean UNDER_LINE_TO_CAMEL = true;// 是否下划线转换为驼峰式
    private static final String TABLES_NAME = "user_msg,order_info";//需要生成的目标表名,若生成所有表则无需填写,多个表名间用,分割

    public static void main(String[] args) {
        /*生成代码生成器*/
        AutoGenerator autoGenerator = new AutoGenerator();
        /*配置文件相关配置*/
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(OUT_PUT_DIR);
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setOpen(SET_OPEN);
        globalConfig.setSwagger2(SET_SWAGGER);
        globalConfig.setServiceName(SERVICE_NAME);
        globalConfig.setEntityName(ENTITY_NAME);
//        globalConfig.setBaseResultMap(true);    //xml开启BaseResultMap
//        globalConfig.setBaseColumnList(true);   //xml开启BaseColumnList
        autoGenerator.setGlobalConfig(globalConfig);
        /*配置数据源相关信息*/
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(DATASOURCE_URL);
        dataSourceConfig.setDriverName(DATASOURCE_DRIVER);
        dataSourceConfig.setUsername(DATASOURCE_USERNAME);
        dataSourceConfig.setPassword(DATASOURCE_PASSWORD);
        autoGenerator.setDataSource(dataSourceConfig);
        /*配置包路径*/
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(PACKAGE_PREFIX);
        autoGenerator.setPackageInfo(packageConfig);
        /*配置模板*/
        TemplateConfig templateConfig = new TemplateConfig();
        autoGenerator.setTemplate(templateConfig);
        /*配置自定义信息*/
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };
        /*策略配置*/
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(UNDER_LINE_TO_CAMEL ? NamingStrategy.underline_to_camel : NamingStrategy.no_change);
        strategyConfig.setEntityLombokModel(SET_LOMBOOK);
        if (StringUtils.isNotEmpty(TABLES_NAME)) strategyConfig.setInclude(TABLES_NAME.split(","));
        //strategyConfig.setControllerMappingHyphenStyle(true);
        // 表前缀
        //strategy.setTablePrefix(pc.getModuleName() + "_");
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.execute();
    }
}
